﻿/* PN264 - A Parallel H.264 Encoding Utility
 * Copyright © 2009 PN264 Development Team
 *
 * This software is released the terms and conditions of the MIT License,
 * a copy of which can be found in the License.txt file.
 *
 * Contributors:
 * Will 'AnarkiNet' Shelley (AnarkiNet@gmail.com)
 */
using System;
using System.Windows.Forms;

namespace PN264.Dialogs
{
    /// <summary>
    /// Represents a dialog that allows the user to configure workers.
    /// </summary>
    public partial class WorkerConfigurationDialog : CommonDialog
    {
        #region Constructors
        public WorkerConfigurationDialog()
        {
            this.form = new WorkerConfigurationDialogForm(this);
            this.DefaultPreset = WorkerPoolCapacityPreset.LogicalCpuCount;
            this.DefaultWorkerCount = -1;
            this.SelectedPreset = this.DefaultPreset;
            this.SelectedWorkerCount = this.DefaultWorkerCount;
        }
        #endregion
        #region Events
        /// <summary>
        /// Raised when the <see cref="WorkerConfigurationDialog.DefaultPreset"/> property changes.
        /// </summary>
        public event EventHandler DefaultPresetChanged;
        /// <summary>
        /// Raised when the <see cref="WorkerConfigurationDialog.DefaultWorkerCount"/> property changes.
        /// </summary>
        public event EventHandler DefaultWorkerCountChanged;
        /// <summary>
        /// Raised when the <see cref="WorkerConfigurationDialog.SelectedPreset"/> property changes.
        /// </summary>
        public event EventHandler SelectedPresetChanged;
        /// <summary>
        /// Raised when the <see cref="WorkerConfigurationDialog.SelectedWorkerCount"/> property changes.
        /// </summary>
        public event EventHandler SelectedWorkerCountChanged;
        #endregion
        #region Fields
        private WorkerPoolCapacityPreset defaultPreset = WorkerPoolCapacityPreset.LogicalCpuCount;
        private WorkerPoolCapacityPreset selectedPreset = WorkerPoolCapacityPreset.LogicalCpuCount;
        private int defaultWorkerCount;
        private int selectedWorkerCount;
        private WorkerConfigurationDialogForm form;
        #endregion
        #region Methods
        protected virtual void OnDefaultPresetChanged(EventArgs e)
        {
            if (this.DefaultPresetChanged != null)
            {
                this.DefaultPresetChanged(this, e);
            }
        }
        protected virtual void OnDefaultWorkerCountChanged(EventArgs e)
        {
            if (this.DefaultWorkerCountChanged != null)
            {
                this.DefaultWorkerCountChanged(this, e);
            }
        }
        protected virtual void OnSelectedPresetChanged(EventArgs e)
        {
            if (this.SelectedPresetChanged != null)
            {
                this.SelectedPresetChanged(this, e);
            }
        }
        protected virtual void OnSelectedWorkerCountChanged(EventArgs e)
        {
            if (this.SelectedWorkerCountChanged != null)
            {
                this.SelectedWorkerCountChanged(this, e);
            }
        }
        public override void Reset()
        {
            this.SelectedPreset = this.DefaultPreset;
            this.SelectedWorkerCount = this.DefaultWorkerCount;

            //TODO: Determine if we need to actually reset anything.
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {

            this.form.ShowDialog();

            if (this.form.DialogResult == DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        #endregion
        #region Properties
        /// <summary>
        /// Gets or sets the <see cref="WorkerCountPreset"/> that is used as the default when the dialog is reset.
        /// </summary>
        /// <remarks>
        /// The <see cref="WorkerConfigurationDialog.DefaultPresetChanged"/> event notifies subscribers when the value of this property changes.
        /// </remarks>
        public WorkerPoolCapacityPreset DefaultPreset
        {
            get
            {
                return this.defaultPreset;
            }
            set
            {
                this.defaultPreset = value;
                this.OnDefaultPresetChanged(null);
            }
        }
        /// <summary>
        /// Gets or sets the worker count that is used as the default when the dialog is reset or first shown.
        /// </summary>
        /// <remarks>
        /// The <see cref="WorkerConfigurationDialog.DefaultWorkerCountChanged"/> event notifies subscribers when the value of this property changes.
        /// </remarks>
        public int DefaultWorkerCount
        {
            get
            {
                return this.defaultWorkerCount;
            }
            set
            {
                this.defaultWorkerCount = value;
                this.OnDefaultWorkerCountChanged(null);
            }
        }
        /// <summary>
        /// Gets or sets the <see cref="WorkerCountPreset"/> that is represents the option selected by the user.
        /// </summary>
        /// <remarks>
        /// The <see cref="WorkerConfigurationDialog.SelectedPresetChanged"/> event notifies subscribers when the value
        /// of this property changes.
        /// </remarks>
        public WorkerPoolCapacityPreset SelectedPreset
        {
            get
            {
                return this.selectedPreset;
            }
            set
            {
                this.selectedPreset = value;
                this.OnSelectedPresetChanged(null);
            }
        }
        /// <summary>
        /// Gets or sets the worker count that represents the value selected by the user.
        /// </summary>
        /// <remarks>
        /// The <see cref="WorkerConfigurationDialog.SelectedWorkerCount"/> event notifies subscribers when the value
        /// of this property changes.
        /// </remarks>
        public int SelectedWorkerCount
        {
            get
            {
                if (this.SelectedPreset == WorkerPoolCapacityPreset.None)
                {
                    return 0;
                }
                else if (this.SelectedPreset == WorkerPoolCapacityPreset.Single)
                {
                    return 1;
                }
                else if (this.SelectedPreset == WorkerPoolCapacityPreset.LogicalCpuCount)
                {
                    return Environment.ProcessorCount;
                }
                else
                {
                    return this.selectedWorkerCount;
                }
            }
            set
            {
                this.selectedWorkerCount = value;
                this.OnSelectedWorkerCountChanged(null);
            }
        }
        #endregion
    }
}
